package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.AdminAdminService;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.util.ZhouWenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/admin")
public class AdminAdminController{
    // 获取Service层对象
    @Autowired
    AdminAdminService adminAdminService;

    /**
     * 管理员删除
     * @param request 请求
     * @param response 响应
     */
    @PostMapping("/delete")
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取Post请求体
        String requestBody = ZhouWenUtil.getRequestBody(request);
        // 数据转对象
        MarketAdmin marketAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        // 数据完善性判断
        if (marketAdmin == null || marketAdmin.getId() == null) {
            Object o = ResponseUtil.badArgument();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 删除管理员的核心操作
        boolean result = adminAdminService.delete(marketAdmin);
        // 判定操作结果
        if (result) {
            // 响应
            Object ok = ResponseUtil.ok();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
        } else {
            // 响应
            Object o = ResponseUtil.updatedDataFailed();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
        }
    }

    /**
     * 管理员更新
     * @param request 请求
     * @param response 响应
     */
    @PostMapping("/update")
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取Post请求体
        String requestBody = ZhouWenUtil.getRequestBody(request);
        // 数据转对象
        MarketAdmin updateMarkAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        // 数据完善性判断
        if (updateMarkAdmin == null || updateMarkAdmin.getId() == null || updateMarkAdmin.getUsername() == null) {
            Object o = ResponseUtil.badArgument();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 数据规则性判断
        if (updateMarkAdmin.getPassword() != null && updateMarkAdmin.getPassword().length() < 4){
            Object o = ResponseUtil.badArgumentValue();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 修改管理员的核心操作
        MarketAdmin marketAdmin = adminAdminService.update(updateMarkAdmin);
        // 判定
        if (marketAdmin == null) {
            Object serious = ResponseUtil.serious();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(serious));
        }
        // 响应
        Object ok = ResponseUtil.ok(marketAdmin);
        response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    /**
     * 管理员添加
     * @param request 请求
     * @param response 响应
     */
    @PostMapping("/create")
    private void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取Post请求体
        String requestBody = ZhouWenUtil.getRequestBody(request);
        // 数据转对象
        MarketAdmin newMarkAdmin = JacksonUtil.getObjectMapper().readValue(requestBody, MarketAdmin.class);
        // 判定条件
        if (newMarkAdmin == null || newMarkAdmin.getUsername() == null || newMarkAdmin.getPassword() == null) {
            Object o = ResponseUtil.badArgument();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 规则判定
        if (newMarkAdmin.getUsername().length() < 2 || newMarkAdmin.getPassword().length() < 4) {
            Object o = ResponseUtil.badArgumentValue();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 创建管理员的核心操作
        MarketAdmin marketAdmin = adminAdminService.create(newMarkAdmin);
        // 判定
        if (marketAdmin == null) {
            Object serious = ResponseUtil.serious();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(serious));
        }
        // 响应
        Object ok = ResponseUtil.ok(marketAdmin);
        response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    /**
     * 管理员查询方法
     * @param request 请求
     * @param response 响应
     */
    @GetMapping("/list")
    private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取各参数数据
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String username = request.getParameter("username");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        // 判空 (用户名为空查询全部)
        if (sort == null || order == null) {
            Object o = ResponseUtil. badArgumentValue();
            response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            return;
        }
        // 查询核心代码
        List<MarketAdmin> adminList = adminAdminService.list(page, limit, username, sort, order);
        // 设置响应对象
        Object ok = ResponseUtil.okList(adminList);
        // 设置响应体
        response.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }
}